package com.im.users.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.im.common.utils.Query;
import com.im.security.dto.LoginVo;
import com.im.users.entity.FansEntity;
import com.im.users.entity.FollowerEntity;
import com.im.users.entity.UserEntity;
import com.im.users.mapper.FollowerDao;
import com.im.users.service.FansService;
import com.im.users.service.FollowerService;
import com.im.users.service.UserService;
import com.im.users.vo.FansVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.im.common.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;


@Slf4j
@Service("followerService")
public class FollowerServiceImpl extends ServiceImpl<FollowerDao, FollowerEntity> implements FollowerService {

    @Autowired
    private UserService userService;

    @Autowired
    private FansService fansService;

    @Autowired
    private FollowerService followerService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FollowerEntity> page = this.page(new Query<FollowerEntity>().getPage(params), new QueryWrapper<FollowerEntity>());
        return new PageUtils(page);
    }

    public Long getFollowerCount(Integer idNum) {
        LambdaQueryWrapper<FollowerEntity> queryWrapper = new LambdaQueryWrapper<FollowerEntity>().eq(FollowerEntity::getUserNum, idNum);
        long followerCount = this.count(queryWrapper);
        return followerCount;
    }

    @Override
    public List<FansVo> getFollower(LoginVo loginVo) {
        LambdaQueryWrapper<FollowerEntity> queryWrapper = new LambdaQueryWrapper<FollowerEntity>().eq(FollowerEntity::getUserNum, loginVo.getIdNum());
        ArrayList<Integer> list = new ArrayList<>();
        List<FollowerEntity> entities = this.list(queryWrapper);
        if (!CollectionUtil.isEmpty(entities)) {
            for (FollowerEntity fansEntity : entities) {
                list.add(fansEntity.getFollowerNum());
            }
        }
        LambdaQueryWrapper<UserEntity> wrapper = new LambdaQueryWrapper<UserEntity>()
                .in(UserEntity::getIdNum, list);
        List<UserEntity> entityList = userService.list(wrapper);
        ArrayList<FansVo> fansVos = new ArrayList<>();
        for (UserEntity userEntity : entityList) {
            FansVo fansVo = new FansVo();
            fansVo.setFollow(1);
            BeanUtils.copyProperties(userEntity, fansVo);
            fansVos.add(fansVo);
        }
        return fansVos;
    }

    @Override
    @Transactional
    public Boolean unFollow(int uid, LoginVo loginVo) {
        LambdaQueryWrapper<UserEntity> eq = new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getId, uid);
        UserEntity follower = userService.getOne(eq);
        LambdaQueryWrapper<FollowerEntity> wrapper = new LambdaQueryWrapper<FollowerEntity>().eq(FollowerEntity::getUserNum, loginVo.getIdNum()).eq(FollowerEntity::getFollowerNum, follower.getIdNum());
        boolean result = this.remove(wrapper);
        log.debug("result:{}", result);
        LambdaQueryWrapper<FansEntity> wrapper1 = new LambdaQueryWrapper<FansEntity>().eq(FansEntity::getUserNum, follower.getIdNum()).eq(FansEntity::getFansNum, loginVo.getIdNum());
        boolean result1 = fansService.remove(wrapper1);
        return result && result1;
    }

    @Override
    @Transactional()
    public Boolean follow(int uid, LoginVo loginVo) {
        UserEntity user = userService.getById(uid);
        FansEntity fansEntity = new FansEntity();
        fansEntity.setFansHead(user.getUserIcon());
        fansEntity.setFansNum(user.getIdNum());
        fansEntity.setFansNickname(user.getUsername());
        fansEntity.setUserNum(loginVo.getIdNum());

        boolean result1 = fansService.saveOrUpdate(fansEntity);
        FollowerEntity followerEntity = new FollowerEntity();
        followerEntity.setFollowerHead(user.getUserIcon());
        followerEntity.setUserNum(loginVo.getIdNum());
        followerEntity.setFollowerNum(user.getIdNum());
        followerEntity.setFollowerNickname(user.getUsername());
        boolean result2 = followerService.saveOrUpdate(followerEntity);
        return result1 && result2;
    }
}